package com.taikang.common.util;

import java.text.ParseException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

/**
 * 
 * @author liucr
 * 
 */
public class RequestUtils {
	
	protected static final Logger logger = Logger.getLogger(RequestUtils.class);
	/**
	 * 
	 * @param request
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public static String getParameter(String name, String defaultValue) {
		HttpServletRequest request = ServletActionContext.getRequest();
		String value = request.getParameter(name);
		if (value == null) {
			value = defaultValue;
		}
		logger.debug("["+name+"].value="+value);
		return value;
	}

	/**
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public static String getParameter(String name) {
		return getParameter(name, "");
	}
	
	public static Date getDateParameter(String name) throws ParseException {
		Date result=null;
		String date= getParameter(name);
		if(StringUtils.isBlank(date))
		return null;
		result=DateUtils.parseDate(date);
		return result;
	}

	/**
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public static int getIntParameter(String name) {
		return getIntParameter(name,0);
	}
	
	public static int getIntParameter(String name,int deValue) {
		String value = getParameter(name);
		if(StringUtils.isBlank(value)){
			return deValue;
		}
		return Integer.parseInt(value);
	}
	
	
	

}
